package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.entity.CommentEty;
import com.lanou.connotationjoke.entity.DataDetialEty;
import com.lanou.connotationjoke.entity.JsonEty;

import java.util.List;

/**
 * Created by dllo on 16/1/11.
 */
public class HomeJokeRecycleViewAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<DataDetialEty> datas;
    private RequestQueue queue;
    private ImageLoader imageLoader;

    public HomeJokeRecycleViewAdapter(Context context, JsonEty jsonEty) {
        this.context = context;
        this.datas = jsonEty.getData().getData();
        queue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType != 1) {
            view = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke, null);
            JokeViewHolder jokeViewHolder = new JokeViewHolder(view);
            return jokeViewHolder;
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke_comment, null);
            JokeWithCommentViewHolder jokeWithCommentViewHolder = new JokeWithCommentViewHolder(view);
            return jokeWithCommentViewHolder;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DataDetialEty ety = datas.get(position);
        if (getItemViewType(position) != 1&&ety.getGroup()!=null) {
            JokeViewHolder jokeViewHolder = (JokeViewHolder) holder;
            jokeViewHolder.diggTv.setText("" + ety.getGroup().getDigg_count());
            jokeViewHolder.contentTv.setText(ety.getGroup().getContent());
            jokeViewHolder.userNameTv.setText(ety.getGroup().getUser().getName());
            jokeViewHolder.categoryName_Tv.setText(ety.getGroup().getCategory_name());
            jokeViewHolder.buryTv.setText("" + ety.getGroup().getBury_count());
            jokeViewHolder.hotCommentTv.setText("" + ety.getGroup().getComment_count());
            jokeViewHolder.sendTv.setText("" + ety.getGroup().getShare_count());
            jokeViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
            jokeViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
            jokeViewHolder.userPictureIv.setImageUrl(ety.getGroup().getUser().getAvatar_url()
                    , imageLoader);
            jokeViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                }
            });


        } else if (ety.getGroup()!=null){
            JokeWithCommentViewHolder commentViewHolder = (JokeWithCommentViewHolder) holder;
            commentViewHolder.userNameTv.setText(ety.getGroup().getUser().getName());
            commentViewHolder.contentTv.setText(ety.getGroup().getContent());
            commentViewHolder.categoryName_Tv.setText(ety.getGroup().getCategory_name());
            commentViewHolder.diggTv.setText("" + ety.getGroup().getDigg_count());
            commentViewHolder.buryTv.setText("" + ety.getGroup().getBury_count());
            commentViewHolder.hotCommentTv.setText("" + ety.getGroup().getComment_count());
            commentViewHolder.sendTv.setText("" + ety.getGroup().getShare_count());
            commentViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
            commentViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
            commentViewHolder.userPictureIv.setImageUrl(ety.getGroup().getUser().getAvatar_url()
                    , imageLoader);
            commentViewHolder.commentUserNameTv.setText(ety.getComments().get(0).getUser_name());
            commentViewHolder.commentDiggCountTv.setText("" + ety.getComments().get(0).getDigg_count());
            commentViewHolder.commentContentTv.setText("" + ety.getComments().get(0).getText());
            commentViewHolder.commentUserPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
            commentViewHolder.commentUserPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
            commentViewHolder.commentUserPictureIv.setImageUrl(ety.getComments().get(0).getAvatar_url(), imageLoader);
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        List<CommentEty> commentEties = datas.get(position).getComments();
        if (commentEties == null) {
            return 0;
        } else if (commentEties.size() >= 1) {
            return 1;
        }
        return 0;
    }

    class JokeViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;

        public JokeViewHolder(View itemView) {
            super(itemView);
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
        }
    }

    class JokeWithCommentViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        NetworkImageView commentUserPictureIv;
        TextView commentUserNameTv;
        TextView commentDiggCountTv;
        TextView commentContentTv;

        public JokeWithCommentViewHolder(View itemView) {
            super(itemView);
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            commentUserPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_comment_user_picture);
            commentUserNameTv = (TextView) itemView.findViewById(R.id.tv_comment_user_name);
            commentDiggCountTv = (TextView) itemView.findViewById(R.id.tv_comment_digg_count);
            commentContentTv = (TextView) itemView.findViewById(R.id.tv_comment_content);
        }
    }
}
